package api

import (
	"ginfast/app/mall/dto"
	"ginfast/app/mall/model"
	"ginfast/pkg/ginx"
	"ginfast/pkg/orm"
	"ginfast/util"
)

// Brand 品牌
type Brand struct {
}

// ListHandler 列表
func (Brand) ListHandler(c *ginx.Context) {
	var p dto.Where
	if err := c.MustBind(&p); err != nil {
		return
	}
	var data []model.Brand
	total, _ := p.DbModelWhere(&model.Brand{}).Find(&data)
	c.JSONWriteTotal(total, data)
}

// GetHandler 查询
func (Brand) GetHandler(c *ginx.Context) {
	util.QueryByID(&model.Brand{}, c)
}

// AddHandler 新增
func (Brand) AddHandler(c *ginx.Context) {
	var p model.Brand
	//获取参数
	err := c.MustBind(&p.BrandOpt)
	if err != nil {
		return
	}
	err = orm.DbCreate(&p)
	c.JSONWriteError(err)
}

// UpdateHandler 更新
func (Brand) UpdateHandler(c *ginx.Context) {
	var p model.Brand
	//获取参数
	err := c.MustBind(&p.BrandOpt)
	if err != nil {
		return
	}
	err = orm.DbUpdateModel(&p)
	c.JSONWriteError(err)
}

type showStatus struct {
	IDS        string `form:"ids"`
	ShowStatus string `form:"showStatus"`
}

func (Brand) UpdateShowHandler(c *ginx.Context) {
	var p showStatus
	err := c.MustBind(&p)
	if err != nil {
		return
	}
	ids := util.StringSplit(p.IDS, ",")
	err = orm.DbUpdateValues(&model.Brand{}, ids, orm.H{"ShowStatus": p.ShowStatus})
	c.JSONWriteError(err)
}

// UpdateFactoryHandler 更新
func (Brand) UpdateFactoryHandler(c *ginx.Context) {
	var p model.Brand
	//获取参数
	err := c.MustBind(&p.BrandOpt)
	if err != nil {
		return
	}
	err = orm.DbUpdateFields(&p, "FactoryStatus")
	c.JSONWriteError(err)
}

// DeleteHandler 删除
func (Brand) DeleteHandler(c *ginx.Context) {
	util.Deletes(&model.Brand{}, c)
}

func (o Brand) Routers(r *ginx.RouterGroup) {
	r.GET("/list", o.ListHandler)
	r.GET("/:id", o.GetHandler)
	r.POST("", o.AddHandler)
	r.PUT("", o.UpdateHandler)
	r.POST("/showStatus", o.UpdateShowHandler)
	r.POST("/factoryStatus", o.UpdateFactoryHandler)
	r.DELETE("", o.DeleteHandler)
}
